package yyy.ab.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import yyy.ab.common.core.BaseController;
import yyy.ab.common.core.ResultBean;
import yyy.ab.common.exception.BadRequestException;
import yyy.ab.logging.annotation.Log;
import yyy.ab.modules.system.entity.SysDict;
import yyy.ab.modules.system.entity.SysDictDetail;
import yyy.ab.modules.system.service.SysDictDetailService;
import yyy.ab.modules.system.service.SysDictService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "系统：字典管理")
@RequestMapping("/system/dict")
public class SysDictController extends BaseController {

    private final SysDictService dictService;
    private final SysDictDetailService dictDetailService;

    /**
     * 字典页面
     */
    @RequestMapping
    public ModelAndView jump() {
        ModelAndView view = new ModelAndView();
        view.setViewName("system/dict/index");
        return view;
    }

    //    @Log("查询字典")
    @ApiOperation("查询字典")
    @GetMapping("/findPage")
    public Object query(@RequestParam Map<String, Object> params) {
        IPage<SysDict> page = dictService.findPage(params);
        return jsonForTable(page);
    }

    @ApiOperation("创建字典")
    @PostMapping
    public Object create(@RequestBody SysDict dict) {
        ResultBean resultBean = dictService.create(dict);
        return resultBean;
    }

    @Log("修改字典")
    @ApiOperation("修改字典")
    @PutMapping
    public Object update(@RequestBody SysDict dict) {
        ResultBean resultBean = dictService.update(dict);
        return resultBean;
    }


    @ApiOperation("删除字典")
    @DeleteMapping
    public Object deleteAll(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        ResultBean resultBean = dictService.deleteAll(idList);
        return resultBean;
    }

    //    @Log("查询字典")
    @ApiOperation("查询字典明细")
    @GetMapping("/findDetailPage")
    public Object detailQuery(@RequestParam Map<String, Object> params) {
        IPage<SysDictDetail> page = dictDetailService.findPage(params);
        return jsonForTable(page);
    }

    @ApiOperation("创建字典详情")
    @PostMapping("/detail")
    public Object createDetail(@RequestBody SysDictDetail dictDetail) {
        ResultBean resultBean = dictDetailService.create(dictDetail);
        return resultBean;
    }

    @ApiOperation("修改字典详情")
    @PutMapping("/detail")
    public Object updateDetail(@RequestBody SysDictDetail dictDetail) {
        ResultBean resultBean = dictDetailService.update(dictDetail);
        return resultBean;
    }


    @ApiOperation("删除字典详情")
    @DeleteMapping("/detail")
    public Object deleteDetailAll(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        ResultBean resultBean = dictDetailService.deleteAll(idList);
        return resultBean;
    }
}
